using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The CurrencyRateTableDB class is responsible for interacting with the database to retrieve and store information
/// about CurrencyRateTable objects.
/// </summary>
public class CurrencyRateTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of CurrencyRateTable from the underlying datasource.
/// </summary>
/// <param name="currencyRateKey">The unique CurrencyRateKey of the CurrencyRateTable in the database.</param>
/// <returns>An CurrencyRateTable when the CurrencyRateKey was found in the database, or null otherwise.</returns>
public static CurrencyRateTable GetItem(Int16 currencyRateKey)
{
CurrencyRateTable myCurrencyRateTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyRateTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@currencyRateKey", currencyRateKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myCurrencyRateTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myCurrencyRateTable;
}
}

/// <summary>
/// Returns a list with CurrencyRateTable objects.
/// </summary>
/// <returns>A generics List with the CurrencyRateTable objects.</returns>
public static CurrencyRateTableList GetList()
{
CurrencyRateTableList tempList = new CurrencyRateTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyRateTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Saves a CurrencyRateTable in the database.
/// </summary>
/// <param name="myCurrencyRateTable">The CurrencyRateTable instance to save.</param>
/// <returns>The new CurrencyRateKey if the CurrencyRateTable is new in the database or the existing CurrencyRateKey when an item was updated.</returns>
public static Int16 Save(CurrencyRateTable myCurrencyRateTable)
{
Int16 result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyRateTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myCurrencyRateTable.CurrencyRateKey == -1)
{myCommand.Parameters.AddWithValue("@currencyRateKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@currencyRateKey", myCurrencyRateTable.CurrencyRateKey);
}
if (myCurrencyRateTable.FromCurrency == null){
myCommand.Parameters.AddWithValue("@fromCurrency", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@fromCurrency", myCurrencyRateTable.FromCurrency);
}
if (myCurrencyRateTable.ToCurrency == null){
myCommand.Parameters.AddWithValue("@toCurrency", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@toCurrency", myCurrencyRateTable.ToCurrency);
}
if (myCurrencyRateTable.Rate == null){
myCommand.Parameters.AddWithValue("@rate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@rate", myCurrencyRateTable.Rate);
}
if (myCurrencyRateTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myCurrencyRateTable.Status);
}
if (myCurrencyRateTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myCurrencyRateTable.CreatedDateTime);
}
if (myCurrencyRateTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myCurrencyRateTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt16(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a CurrencyRateTable from the database.
/// </summary>
/// <param name="currencyRateKey">The CurrencyRateKey of the CurrencyRateTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(Int16 currencyRateKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocCurrencyRateTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@currencyRateKey", currencyRateKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the CurrencyRateTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static CurrencyRateTable FillDataRecord(IDataRecord myDataRecord )
{
CurrencyRateTable myCurrencyRateTable = new CurrencyRateTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyRateKey")))
{
myCurrencyRateTable.CurrencyRateKey = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CurrencyRateKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FromCurrency")))
{
myCurrencyRateTable.FromCurrency = myDataRecord.GetString(myDataRecord.GetOrdinal("FromCurrency"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ToCurrency")))
{
myCurrencyRateTable.ToCurrency = myDataRecord.GetString(myDataRecord.GetOrdinal("ToCurrency"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Rate")))
{
myCurrencyRateTable.Rate = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Rate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myCurrencyRateTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myCurrencyRateTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myCurrencyRateTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myCurrencyRateTable;
}
}
